package com.yic.module.mes.dal.mysql.reportworkhours;

import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yic.framework.mybatis.core.mapper.BaseMapperX;
import com.yic.module.mes.dal.dataobject.itemproduce.ItemProduceDO;
import com.yic.module.mes.dal.dataobject.mo.MoDO;
import com.yic.module.mes.dal.dataobject.report.ReportDO;
import com.yic.module.mes.dal.dataobject.reportworkhours.ReportWorkHoursDO;
import org.apache.ibatis.annotations.Mapper;
import com.yic.module.mes.controller.admin.reportworkhours.vo.*;

/**
 * 报工工时 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface ReportWorkHoursMapper extends BaseMapperX<ReportWorkHoursDO> {

    default PageResult<ReportWorkHoursDO> selectPage(ReportWorkHoursPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ReportWorkHoursDO>()
                .eqIfPresent(ReportWorkHoursDO::getReportId, reqVO.getReportId())
                .eqIfPresent(ReportWorkHoursDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ReportWorkHoursDO::getWorkHours, reqVO.getWorkHours())
                .eqIfPresent(ReportWorkHoursDO::getTotal, reqVO.getTotal())
                .betweenIfPresent(ReportWorkHoursDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ReportWorkHoursDO::getId));
    }

    default List<ReportWorkHoursDO> selectList(ReportWorkHoursExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ReportWorkHoursDO>()
                .eqIfPresent(ReportWorkHoursDO::getReportId, reqVO.getReportId())
                .eqIfPresent(ReportWorkHoursDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ReportWorkHoursDO::getWorkHours, reqVO.getWorkHours())
                .eqIfPresent(ReportWorkHoursDO::getTotal, reqVO.getTotal())
                .betweenIfPresent(ReportWorkHoursDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ReportWorkHoursDO::getId));
    }

    default int deleteByReportId(Long id) {
        return delete(new LambdaQueryWrapperX<ReportWorkHoursDO>()
                .eq(ReportWorkHoursDO::getReportId, id));
    }

    default List<ReportWorkHoursFormsRespVO> selectForms(ReportWorkHoursFormsReqVO reqVO) {
        return selectJoinList(ReportWorkHoursFormsRespVO.class, new MPJLambdaWrapper<ReportWorkHoursDO>()
                .select(ReportWorkHoursDO::getWorkHours)
                .select(ReportWorkHoursDO::getTotal)
                .selectSum(ReportWorkHoursDO::getTotal, ReportWorkHoursFormsRespVO::getTotalSum)
                .select(ReportDO::getFactoryId)
                .select(ReportDO::getMoId)
                .select(ReportDO::getItemId)
                .select(ReportDO::getReportAmount)
                .selectSum(ReportDO::getReportAmount, ReportWorkHoursFormsRespVO::getReportAmountSum)
                .select(ReportDO::getWorkshopId)
                .select(ReportDO::getCreateTime)
                .select(ReportDO::getUserId)
                .select(ReportDO::getProcessId)
                .selectAs(MoDO::getCode, ReportWorkHoursFormsRespVO::getMoCode)
                .select(MoDO::getBatchNo)
                .select(MoDO::getRemark)
                .leftJoin(ReportDO.class, ReportDO::getId, ReportWorkHoursDO::getReportId)
                .leftJoin(MoDO.class, MoDO::getId, ReportDO::getMoId)
                .eq(ObjectUtil.isNotEmpty(reqVO.getWorkshopId()), ReportDO::getWorkshopId, reqVO.getWorkshopId())
                .eq(ObjectUtil.isNotEmpty(reqVO.getMoId()), ReportDO::getMoId, reqVO.getMoId())
                .eq(ObjectUtil.isNotEmpty(reqVO.getMoCode()), ReportDO::getMoCode, reqVO.getMoCode())
                .eq(ObjectUtil.isNotEmpty(reqVO.getUserId()), ReportDO::getUserId, reqVO.getUserId())
                .between(ObjectUtil.isNotEmpty(reqVO.getStartTime()) && ObjectUtil.isNotEmpty(reqVO.getEndTime()), ReportDO::getCreateTime, reqVO.getStartTime(), reqVO.getEndTime())
                .groupBy(ReportDO::getFactoryId, ReportDO::getWorkshopId, ReportDO::getMoId, ReportDO::getUserId, ReportDO::getProcessId));
    }
}
